<?

class uploader {

// FUNCTION // UPLOAD FILE check for errors and fet extension ==============
function upload_file($field_name, $max_file_size){

	$error = null;
	
	if($_FILES[$field_name]['size'] > $max_file_size) {
		$error = "File size is larger then ".display_file_size($max_file_size);
	}
	
	if(!is_uploaded_file($_FILES[$field_name]['tmp_name'])){
		$error = "File did not upload!";
	}
	
	// get extension - before it becomes a .tmp file
		$ext = strrchr($_FILES[$field_name]['name'], ".");
		
		//echo $_FILES[$field_name]['tmp_name'];
		
		$results_array = array("error" => $error, "source" => $_FILES[$field_name]['tmp_name'], "ext" => $ext );
		
		return $results_array;
		
		// maybe move images to a temp folder?
	
	
}
// END FUNCTION ==============================================================




// FUNCTION // MOVE FILE ======================================================
function move_file($source, $destination){

	move_uploaded_file($source, $destination); // only for uploads????

	
}
// END FUNCTION ============================================================





// FUNCTION // OPTIMISE THE IMAGE ===========================================
function optimise_logo_image($height_allowance, $width_allowance, $source_file, $output_url, $output_file_name, $ext){

	// get the size of the uploaded image
	list($width, $height) = getimagesize($source_file);


	// get the new dimensions
	$wh_src = $width / $height;
	$wh_dest = $width_allowance / $height_allowance;

	if ($height >= round($width / $wh_dest)){

		$width_new = round($height * $wh_dest);
		$height_new = round($height);
		$x = ($width - $width_new)/2;
		$y = "0";

		// border values
		$scale_ratio = ($height/$height_allowance);
		$border_height = ceil($height_allowance);
		$scaled_width = ceil($width/$scale_ratio);
		$border_width = ceil(($width_allowance-$scaled_width)/2);

		$x_1_start_border = 0;
		$y_1_start_border = 0;
		$x_1_end_border = $border_width;
		$y_1_end_border = $border_height;

		$x_2_start_border = floor(($width_allowance - $border_width)-1);
		$y_2_start_border = 0;
		$x_2_end_border = $width_allowance;
		$y_2_end_border = $border_height;

	}else{

		$width_new = round($width);
		$height_new = round($width / $wh_dest);
		$x = "0";
		$y = ($height - $height_new)/2;

		// border values		
		$scale_ratio = ($width/$width_allowance);
		$border_width = ceil($width_allowance);
		$scaled_height = ceil($height/$scale_ratio);
		$border_height = ceil(($height_allowance-$scaled_height)/2);

		$x_1_start_border = 0;
		$y_1_start_border = 0;
		$x_1_end_border = $border_width;
		$y_1_end_border = $border_height;

		$x_2_start_border = 0;
		$y_2_start_border = floor(($height_allowance - $border_height)-1);
		$x_2_end_border = $width_allowance;
		$y_2_end_border = $height_allowance;

	}

	// get the imge type and create a php ime source
	switch ($ext){
		case ".gif": case ".GIF":
			$source_image = imagecreatefromgif($source_file) or die($error = "Unsuported file format");
		break;
		case ".jpg": case ".jpeg": case ".JPG": case ".JPEG":
			$source_image = imagecreatefromjpeg($source_file) or die($error = "Unsuported file format");
		break;
		case ".png": case ".PNG":
			$source_image = imagecreatefrompng($source_file) or die($error = "Unsuported file format");
		break;
		default;
			die($error = "Unsuported file format");
		break;
	}

	// create an image file on the server
	$destination_image = imagecreatetruecolor($width_allowance, $height_allowance);

	// make the bg white
	$white = imagecolorallocate($destination_image, 255, 255, 255);
	imagefill($destination_image,'0','0', $white);
      
	// resize image
	imagecopyresampled($destination_image, $source_image, 0, 0, $x, $y, $width_allowance, $height_allowance, $width_new, $height_new);

	// work around to remove the black borders 
	imagefilledrectangle($destination_image, $x_1_start_border, $y_1_start_border, $x_1_end_border, $y_1_end_border, $white);
	imagefilledrectangle($destination_image, $x_2_start_border, $y_2_start_border, $x_2_end_border, $y_2_end_border, $white);

	// work around to remove the black borders 
	//imagefilltoborder($destination_image, 0, 0, $white, $white);
	//imagefilltoborder($destination_image, $height_allowance, $width_allowance, $white, $white);

	// get destination file name
	$output_file_url_name = $output_url.$output_file_name.".jpg";  	

	// optimise and save image
	imagejpeg($destination_image, $output_file_url_name, '93');
	
	// delete files from server memory
	imagedestroy($source_image);
	imagedestroy($destination_image);
	
	return $error;

} 
// END FUNCTION ==========================================================







// FUNCTION // OPTIMISE THE IMAGE ===========================================
function optimise_photo_image($height_allowance, $width_allowance, $source_file, $output_url, $output_file_name, $ext){


	// get the size of the uploaded image
	list($width, $height) = getimagesize($source_file);

	// get the new dimensions
	$wh_src = $width / $height;
	$wh_dest = $width_allowance / $height_allowance;

	if ($wh_src > $wh_dest){
		$width_new = round($height * $wh_dest);
		$height_new = round($height);
		$x = ($width - $width_new)/2;
		$y = "0";
	}else{
		$width_new = round($width);
		$height_new = round($width / $wh_dest);
		$x = "0";
		$y = ($height - $height_new)/2;
	}	

	// get the imge type and create a php ime source
	switch ($ext){
		case ".gif": case ".GIF":
			$source_image = imagecreatefromgif($source_file) or die($error = "Unsuported file format");
		break;
		case ".jpg": case ".jpeg": case ".JPG": case ".JPEG":
			$source_image = imagecreatefromjpeg($source_file) or die($error = "Unsuported file format");
		break;
		case ".png": case ".PNG":
			$source_image = imagecreatefrompng($source_file) or die($error = "Unsuported file format");
		break;
		default;
			die($error = "Unsuported file format");
		break;
	}

	// create an image file on the server
	$destination_image = imagecreatetruecolor($width_allowance, $height_allowance);

	// make the bg white
	$white = imagecolorallocate($destination_image, 255, 255, 255);
	imagefill($destination_image,'0','0', $white);
      
	// resize image
	imagecopyresampled($destination_image, $source_image, 0, 0, $x, $y, $width_allowance, $height_allowance, $width_new, $height_new);

	// get destination file name
	$output_file_url_name = $output_url.$output_file_name.".jpg";  	

	// optimise and save image
	imagejpeg($destination_image, $output_file_url_name, '93');
	
	// delete files from server memory
	imagedestroy($source_image);
	imagedestroy($destination_image);
	
	return $error;

} 
// END FUNCTION ==========================================================





}
?>